// =========================================================
// 오류 내역 카운트 Grid
// =========================================================
var httpProxyTypeCount = new Ext.data.HttpProxy({
	method: 'POST',
	url: '',
	timeout: 90000
});

var storeTypeCount = new Ext.data.JsonStore({
	root: 'json_list',
	totalProperty: 'json_count',
	idProperty: 'file_nm',
	fields: [
	         'file_no', 'detect_type', 'detect_name', 'cnt'
	         ],
	//autoLoad: true,
	proxy: httpProxyTypeCount
});

function fnRdrType(val, meta, rec, rIdx, cIdx, store)
{
	var strRet = val;
	
	if (rIdx == 4)
	{
		meta.css = 'cellcolor';
		strRet = '<b>' + val + '</b>';
	}
	
	return strRet;
}	

var gridTypeCount = new Ext.grid.GridPanel({
	id: 'id_grid_type_count',
	region: 'center',
	title: '상세 점검내용',
	border: false,
	iconCls: 'icon-grid',
	store: storeTypeCount,
	columns: [{
		id: 'detect_name',
		dataIndex: 'detect_name',
		header: '주요 취약점',
		renderer: fnRdrType
	},{
		id: 'cnt',
		dataIndex: 'cnt',
		renderer: fnRdrType,
		width: 50
	}],
	stripeRows: true,
	autoExpandColumn: 'detect_name',
	height: 150
});



// =========================================================
// 라인정보 Grid
// =========================================================
var httpProxyTypeDetail = new Ext.data.HttpProxy({
	method: 'POST',
	url: '',
	timeout: 90000
});

var storeTypeDetail = new Ext.data.JsonStore({
	root: 'json_list',
	totalProperty: 'json_count',
	idProperty: 'file_no',
	fields: [
		'file_no', 'file_nm', 'line_no', 'line_text', 'security_code'
	],
	//autoLoad: true,
	proxy: httpProxyTypeDetail
});

function fnRdrLine(val, metadata, record, rowIndex, colIndex, store)
{
	var strSource = record.data.security_code
	
	// 모든 Ascii문자를 대상으로 (")문자가 1번이상 나타난 경우 (&quot;)문자로 Replace한다.
	var strReplaceValues = strSource.replace(/["]/g, "&quot;");
	
	var strText = '<b>Replace 될 예상 소스코드</b><br>';
	
	var arrTemp = strReplaceValues.split("     ");
	for (var i=0; i < arrTemp.length; i++)
	{
		// 공백 5자리로 줄바꿈을 처리한다.
		strText = strText + '<br>' + arrTemp[i];
	}
	metadata.attr = 'ext:qtip="' + strText + '"';
	
	return val;
}

var selectModelTypeDetail = new Ext.grid.CheckboxSelectionModel();

var columnModelTypeDetail = new Ext.grid.ColumnModel([
	{
		id: 'file_no',
		header: '파일번호',
		dataIndex: 'file_no',
		hidden: true
	},{
		id: 'line_no',
		header: '라인',
		dataIndex: 'line_no',
		align: 'right',
		width: 80
	},{
		id: 'line_text',
		header: '소스',
		dataIndex: 'line_text',
		renderer: fnRdrLine
	},
	selectModelTypeDetail
]);

var gridTypeDetail = new Ext.grid.GridPanel({
	region: 'center',
	id: 'id_grid_type_detail',
	border: false,
	iconCls: 'icon-grid',
	store: storeTypeDetail,
	cm: columnModelTypeDetail,
	sm: selectModelTypeDetail,
	height: 600,
	loadMask: true,
	stripeRows: true,
	autoExpandColumn: 'line_text',
	buttons: [{
		id: 'id_btn_clear',
		text: '선택취소',
		handler: function(){
			storeTypeDetail.load();
		}
	},{
		id: 'id_btn_fix',
		text: '자동 FIX'
	}]
});


Ext.getCmp('id_btn_fix').on('click', function(){

	var objArray = Ext.getCmp('id_grid_type_detail').getSelectionModel().getSelections();
	if(objArray.length == 0)
	{
		Ext.MessageBox.alert('확인', '선택된 라인이 없습니다.');
		return;
	}
	else
	{
		Ext.MessageBox.confirm('확인', '선택된 '+ objArray.length + '개의 라인을 수정하시겠습니까?', function(btn){
			if (btn == 'no')
			{
				return;
			}
			else
			{
				var objTempList = new Array();
				for (var i=0; i < objArray.length; i++)
				{
					var objTemp = {
							'file_no': objArray[i].get('file_no'),
							'line_no': objArray[i].get('line_no')
					}
					objTempList.push(objTemp);
				}
				
				var objParam = {
						'json_list': objTempList,
						'json_count': objTempList.length
				}
				var jsonParam = Ext.util.JSON.encode(objParam);
				var url = m_ContextName + '/analysis/ajax_detect_fix.code';
				
				fnCodeSave(jsonParam, url);
			}
		});
	}
});
